﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.BLL;

namespace WXYW
{
    public partial class SFTJBaseOnProvider : System.Web.UI.Page
    {
        public string TotalString { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialDropDown();
                BindProviderTJ();
            }
        }

        private void BindProviderTJ()
        {
            ApplicationProductService service = new ApplicationProductService();
            providerRepeater.DataSource = service.GetSFTJProviderModelByYear(Convert.ToInt32(totalYearDropDownList.SelectedValue));
            providerRepeater.DataBind();

            TotalString = string.Format("<td>￥{0}元</td>",service.GetSFTJProviderModelByYear(Convert.ToInt32(totalYearDropDownList.SelectedValue)).Sum(s => s.InAll));
        }

        protected void totalYearDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindProviderTJ();
        }

        private void InitialDropDown()
        {
            for (int i = 2010; i <= DateTime.Now.Year; i++)
            {
                totalYearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            totalYearDropDownList.SelectedIndex = totalYearDropDownList.Items.Count - 1;

            for (int i = 2010; i <= DateTime.Now.Year; i++)
            {
                yearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            yearDropDownList.SelectedIndex = 0;

            for (int i = 2010; i <= DateTime.Now.Year; i++)
            {
                tyearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            tyearDropDownList.SelectedIndex = tyearDropDownList.Items.Count - 1;

            for (int i = 1; i <= 12; i++)
            {
                monthDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            monthDropDownList.SelectedIndex = 0;

            for (int i = 1; i <= 12; i++)
            {
                tmonthDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            tmonthDropDownList.SelectedIndex = DateTime.Now.Month - 1;

            ProviderService service = new ProviderService();
            serviceStationDropDownList.DataSource = service.GetAllProvider();
            serviceStationDropDownList.DataTextField = "ProviderName";
            serviceStationDropDownList.DataValueField = "ProviderCode";
            serviceStationDropDownList.DataBind();
            IntialText();
        }

        private void IntialText()
        {
            yearLabel.Text = yearDropDownList.SelectedValue;
            monthLabel.Text = monthDropDownList.SelectedValue;
            tyearLabel.Text = tyearDropDownList.SelectedValue;
            tmonthLabel.Text = tmonthDropDownList.SelectedValue;
            serviceLabel.Text = serviceStationDropDownList.SelectedItem.Text;
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            IntialText();
            BindProduct();
            BindProviderTJ();
        }

        private void BindProduct()
        {
            ApplicationProductService service = new ApplicationProductService();
            List<ApplicationProduct> products = service.GetProductReturnToFactoryByInterval(
                Convert.ToInt32(yearDropDownList.SelectedValue), 
                Convert.ToInt32(monthDropDownList.SelectedValue), 
                Convert.ToInt32(tyearDropDownList.SelectedValue), 
                Convert.ToInt32(tmonthDropDownList.SelectedValue), 
                serviceStationDropDownList.SelectedValue);

            //foreach (ApplicationProduct product in products)
            //{
            //    if (!product.ApplicationFormReference.IsLoaded)
            //    {
            //        product.ApplicationFormReference.Load();
            //    }
            //    if (!product.ProductTypeReference.IsLoaded)
            //    {
            //        product.ProductTypeReference.Load();
            //    }
            //}

            productGridView.DataSource = products;
            productGridView.DataBind();

            totalLabel.Text = products.Sum(p => p.Cost).ToString();
            resultLabel.Text = totalLabel.Text;
        }

        protected void viewLinkButton_Click(object sender, EventArgs e)
        {
            hfProductID.Value = ((LinkButton)sender).CommandArgument;
            maintainRecordModalPopupExtender.Show();
            BindProductInfo();
        }

        #region maintain record

        private void BindProductInfo()
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(hfProductID.Value));
            blxxTextBox.Text = product.BLXX;
            bzTextBox.Text = product.Remark;
            rmaMaintainLabel.Text = product.ApplicationForm.ApplicationCode;
            typeLabel.Text = product.ProductType.ProductTypeName;
            serialLabel.Text = product.ProductSerialNumber;
            versionNOLbl.Text = product.ProductVersionNumber;
            purchaseTimeLbl.Text = product.ProductPurchaseDateTime.Value.ToString("yyyy-MM-dd");
            wxwcrqLabel.Text = product.RepairCompleteDateTime != null ? Convert.ToDateTime(product.RepairCompleteDateTime).ToString("yyyy-MM-dd") : DateTime.Now.ToString("yyyy-MM-dd");

            /// Chen Pu: 2010-06-02 报价信息未能显示
            //if (product.Cost != null)
            //{
            //    bjLabel.Text = product.Cost.ToString();
            //}
            //else
            //{
            //    bjLabel.Text = "未报价";
            //}
            if (!product.QuatationForm.IsLoaded)
            {
                product.QuatationForm.Load();
            }
            if (product.QuatationForm != null && product.QuatationForm.Count > 0)
            {
                double cost = 0;
                int index = 1;
                foreach (QuatationForm quatation in product.QuatationForm)
                {
                    cost += quatation.Cost;

                    switch (index)
                    {
                        case 1:
                            blljTextBox1.Text = quatation.AccessoryName;
                            break;
                        case 2:
                            blljTextBox2.Text = quatation.AccessoryName;
                            break;
                        case 3:
                            blljTextBox3.Text = quatation.AccessoryName;
                            break;
                        case 4:
                            blljTextBox4.Text = quatation.AccessoryName;
                            break;
                        case 5:
                            blljTextBox5.Text = quatation.AccessoryName;
                            break;
                    }
                    index++;
                }
                bjLabel.Text = string.Format("总金额:{0}", cost);

                WXLJGridView.DataSource = product.QuatationForm;
                WXLJGridView.DataBind();
            }
            else
            {
                bjLabel.Text = "未报价";
            }

            if (!string.IsNullOrEmpty(product.ChangeType))
            {
                if (product.ChangeType.Equals("更换"))
                {
                    ghRadioButton.Checked = true;
                }
                if (product.ChangeType.Equals("替换"))
                {
                    thRadioButton.Checked = true;
                }
            }

            if (!string.IsNullOrEmpty(product.ReplaceProductSerialNumber))
            {
                thSerialNumberTextBox.Text = product.ReplaceProductSerialNumber;
            }

            if (!string.IsNullOrEmpty(product.ReplaceProductType))
            {
                thProductTypeTextBox.Text = product.ReplaceProductType;
            }

            if (!string.IsNullOrEmpty(product.ReplaceProductVersion))
            {
                versionDDL.SelectedValue = product.ReplaceProductVersion;
            }

            if (!string.IsNullOrEmpty(product.RepairAccessory))
            {
                InitialRepaireAccessory(product.RepairAccessory);
            }

            if (!string.IsNullOrEmpty(product.AccessoryPlace))
            {
                InitialAccessoryPlace(product.AccessoryPlace);
            }

            if (!string.IsNullOrEmpty(product.ReasonToProblem))
            {
                InitializeBLYY(product.ReasonToProblem);
            }

            if (!product.ProblemAnalyzeReference.IsLoaded)
            {
                product.ProblemAnalyzeReference.Load();
            }

            if (product.ProblemAnalyze != null)
            {
                InitializeProblemAnalyze(product.ProblemAnalyze);
            }

            InitialProductVersion();
        }

        private void InitializeProblemAnalyze(ProblemAnalyze problemAnalyze)
        {
            SetProblemAnalyzeA(problemAnalyze.A);
            SetProblemAnalyzeB(problemAnalyze.B);
            SetProblemAnalyzeC(problemAnalyze.C);
            SetProblemAnalyzeD(problemAnalyze.D);
            SetProblemAnalyzeE(problemAnalyze.E);
        }

        private void InitialProductVersion()
        {
            ProductClassService classService = new ProductClassService();
            versionDDL.DataSource = classService.GetVersions();
            versionDDL.DataTextField = "Name";
            versionDDL.DataValueField = "Name";
            versionDDL.DataBind();
        }

        private void InitialAccessoryPlace(string accessoryPlace)
        {
            string[] accessorys = accessoryPlace.Split(';');
            for (int i = 0; i < accessorys.Count(); i++)
            {
                switch (i)
                {
                    case 0:
                        ljwzTextBox1.Text = accessorys[i];
                        break;
                    case 1:
                        ljwzTextBox2.Text = accessorys[i];
                        break;
                    case 2:
                        ljwzTextBox3.Text = accessorys[i];
                        break;
                    case 3:
                        ljwzTextBox4.Text = accessorys[i];
                        break;
                    case 4:
                        ljwzTextBox5.Text = accessorys[i];
                        break;
                }
            }
        }

        private void InitializeBLYY(string blyy)
        {
            string[] blyyArray = blyy.Split(';');
            for (int i = 0; i < blyyArray.Count(); i++)
            {
                switch (i)
                {
                    case 0:
                        blyyRadioButtonList1.SelectedValue = blyyArray[i];
                        break;
                    case 1:
                        blyyRadioButtonList2.SelectedValue = blyyArray[i];
                        break;
                    case 2:
                        blyyRadioButtonList3.SelectedValue = blyyArray[i];
                        break;
                    case 3:
                        blyyRadioButtonList4.SelectedValue = blyyArray[i];
                        break;
                    case 4:
                        blyyRadioButtonList4.SelectedValue = blyyArray[i];
                        break;
                }
            }
        }

        private void InitialRepaireAccessory(string accessory)
        {
            string[] accessorys = accessory.Split(';');
            for (int i = 0; i < accessorys.Count(); i++)
            {
                switch (i)
                {
                    case 0:
                        blljTextBox1.Text = accessorys[i];
                        break;
                    case 1:
                        blljTextBox2.Text = accessorys[i];
                        break;
                    case 2:
                        blljTextBox3.Text = accessorys[i];
                        break;
                    case 3:
                        blljTextBox4.Text = accessorys[i];
                        break;
                    case 4:
                        blljTextBox5.Text = accessorys[i];
                        break;
                }
            }
        }


        private string getLJWZ()
        {
            string ljwz = string.Empty;
            if (!string.IsNullOrEmpty(ljwzTextBox1.Text))
            {
                ljwz += ljwzTextBox1.Text;
            }
            if (!string.IsNullOrEmpty(ljwzTextBox2.Text))
            {
                if (!string.IsNullOrEmpty(ljwz))
                {
                    ljwz += ";";
                }
                ljwz += ljwzTextBox2.Text;
            }
            if (!string.IsNullOrEmpty(ljwzTextBox3.Text))
            {
                if (!string.IsNullOrEmpty(ljwz))
                {
                    ljwz += ";";
                }
                ljwz += ljwzTextBox3.Text;
            }
            if (!string.IsNullOrEmpty(ljwzTextBox4.Text))
            {
                if (!string.IsNullOrEmpty(ljwz))
                {
                    ljwz += ";";
                }
                ljwz += ljwzTextBox4.Text;
            }
            if (!string.IsNullOrEmpty(ljwzTextBox5.Text))
            {
                if (!string.IsNullOrEmpty(ljwz))
                {
                    ljwz += ";";
                }
                ljwz += ljwzTextBox5.Text;
            }
            return ljwz;
        }

        private string getWXLJ()
        {
            string wxlj = string.Empty;
            if (!string.IsNullOrEmpty(blljTextBox1.Text))
            {
                wxlj += blljTextBox1.Text;
            }
            if (!string.IsNullOrEmpty(blljTextBox2.Text))
            {
                if (!string.IsNullOrEmpty(wxlj))
                {
                    wxlj += ";";
                }

                wxlj += blljTextBox2.Text;
            }
            if (!string.IsNullOrEmpty(blljTextBox3.Text))
            {
                if (!string.IsNullOrEmpty(wxlj))
                {
                    wxlj += ";";
                }

                wxlj += blljTextBox3.Text;
            }
            if (!string.IsNullOrEmpty(blljTextBox4.Text))
            {
                if (!string.IsNullOrEmpty(wxlj))
                {
                    wxlj += ";";
                }

                wxlj += blljTextBox4.Text;
            }
            if (!string.IsNullOrEmpty(blljTextBox5.Text))
            {
                if (!string.IsNullOrEmpty(wxlj))
                {
                    wxlj += ";";
                }

                wxlj += blljTextBox5.Text;
            }

            return wxlj;
        }

        private string getBLYY()
        {
            string blyy = string.Empty;
            if (blyyRadioButtonList1.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList1.SelectedValue;
            }
            if (blyyRadioButtonList2.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList2.SelectedValue;
            }
            if (blyyRadioButtonList3.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList3.SelectedValue;
            }
            if (blyyRadioButtonList4.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList4.SelectedValue;
            }
            if (blyyRadioButtonList5.SelectedItem != null)
            {
                if (!string.IsNullOrEmpty(blyy))
                {
                    blyy += ";";
                }
                blyy += blyyRadioButtonList5.SelectedValue;
            }

            return blyy;
        }

        #region for problem analyze

        private string getProblemAnalyzeE()
        {
            string eStr = string.Empty;
            if (RadioButton11.Checked)
            {
                eStr = RadioButton11.Text;
            }
            return eStr;
        }

        private void SetProblemAnalyzeE(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton11.Text))
                {
                    RadioButton11.Checked = true;
                }
            }
        }

        private string getProblemAnalyzeD()
        {
            string dStr = string.Empty;
            if (RadioButton10.Checked)
            {
                dStr = RadioButton10.Text;
            }

            return dStr;
        }

        private void SetProblemAnalyzeD(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton10.Text))
                {
                    RadioButton10.Checked = true;
                }
            }
        }

        private string getProblemAnalyzeC()
        {
            string cStr = string.Empty;
            if (RadioButton9.Checked)
            {
                cStr = RadioButton9.Text;
            }
            else if (RadioButton8.Checked)
            {
                cStr = RadioButton8.Text;
            }
            else if (RadioButton13.Checked)
            {
                cStr = RadioButton13.Text;
            }

            return cStr;
        }

        private void SetProblemAnalyzeC(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton9.Text))
                {
                    RadioButton9.Checked = true;
                }
                if (value.Equals(RadioButton8.Text))
                {
                    RadioButton8.Checked = true;
                }
                if (value.Equals(RadioButton13.Text))
                {
                    RadioButton13.Checked = true;
                }
            }
        }

        private string getProblemAnalyzeB()
        {
            string bStr = string.Empty;
            if (RadioButton7.Checked)
            {
                bStr = RadioButton7.Text;
            }
            else if (RadioButton6.Checked)
            {
                bStr = RadioButton6.Text;
            }
            else if (RadioButton5.Checked)
            {
                bStr = RadioButton5.Text;
            }
            else if (RadioButton12.Checked)
            {
                bStr = RadioButton12.Text;
            }
            return bStr;
        }

        private void SetProblemAnalyzeB(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton5.Text))
                {
                    RadioButton5.Checked = true;
                }
                if (value.Equals(RadioButton6.Text))
                {
                    RadioButton6.Checked = true;
                }
                if (value.Equals(RadioButton7.Text))
                {
                    RadioButton7.Checked = true;
                }
                if (value.Equals(RadioButton12.Text))
                {
                    RadioButton12.Checked = true;
                }
            }
        }

        private string getProblemAnalyzeA()
        {
            string aStr = string.Empty;
            if (RadioButton4.Checked)
            {
                aStr = RadioButton4.Text;
            }
            else if (RadioButton3.Checked)
            {
                aStr = RadioButton3.Text;
            }
            else if (RadioButton2.Checked)
            {
                aStr = RadioButton2.Text;
            }
            else if (RadioButton1.Checked)
            {
                aStr = RadioButton1.Text;
            }
            return aStr;
        }

        private void SetProblemAnalyzeA(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Equals(RadioButton1.Text))
                {
                    RadioButton1.Checked = true;
                }
                if (value.Equals(RadioButton2.Text))
                {
                    RadioButton2.Checked = true;
                }
                if (value.Equals(RadioButton3.Text))
                {
                    RadioButton3.Checked = true;
                }
                if (value.Equals(RadioButton4.Text))
                {
                    RadioButton4.Checked = true;
                }
            }
        }

        #endregion

        
        #endregion
    }
}
